import 'word_card.dart';

class WordLibrary {
  final String bookId;
  final List<WordCard> newWords; // 新词词库
  final List<WordCard> reviewWords; // 复习词库

  WordLibrary({
    required this.bookId,
    required this.newWords,
    required this.reviewWords,
  });

  factory WordLibrary.empty(String bookId) {
    return WordLibrary(
      bookId: bookId,
      newWords: [],
      reviewWords: [],
    );
  }

  factory WordLibrary.fromJson(Map<String, dynamic> json) {
    return WordLibrary(
      bookId: json['book_id'],
      newWords: (json['new_words'] as List)
          .map((wordJson) => WordCard.fromJson(wordJson))
          .toList(),
      reviewWords: (json['review_words'] as List)
          .map((wordJson) => WordCard.fromJson(wordJson))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'book_id': bookId,
      'new_words': newWords.map((word) => word.toJson()).toList(),
      'review_words': reviewWords.map((word) => word.toJson()).toList(),
    };
  }

  // 将单词从新词词库移动到复习词库
  WordLibrary moveToReview(String word) {
    final int index = newWords.indexWhere((w) => w.word == word);
    if (index == -1) return this;

    final WordCard wordCard = newWords[index].copyWith(status: 'review');
    final List<WordCard> updatedNewWords = List.from(newWords)..removeAt(index);
    final List<WordCard> updatedReviewWords = List.from(reviewWords)..add(wordCard);

    return WordLibrary(
      bookId: bookId,
      newWords: updatedNewWords,
      reviewWords: updatedReviewWords,
    );
  }

  // 将单词从复习词库移动到新词词库
  WordLibrary moveToNew(String word) {
    final int index = reviewWords.indexWhere((w) => w.word == word);
    if (index == -1) return this;

    final WordCard wordCard = reviewWords[index].copyWith(status: 'new', repeatCount: 0);
    final List<WordCard> updatedReviewWords = List.from(reviewWords)..removeAt(index);
    final List<WordCard> updatedNewWords = List.from(newWords)..add(wordCard);

    return WordLibrary(
      bookId: bookId,
      newWords: updatedNewWords,
      reviewWords: updatedReviewWords,
    );
  }

  // 更新单词的重复次数
  WordLibrary updateRepeatCount(String word, int count) {
    final int newIndex = newWords.indexWhere((w) => w.word == word);
    if (newIndex != -1) {
      final List<WordCard> updatedNewWords = List.from(newWords);
      updatedNewWords[newIndex] = newWords[newIndex].copyWith(repeatCount: count);
      
      return WordLibrary(
        bookId: bookId,
        newWords: updatedNewWords,
        reviewWords: reviewWords,
      );
    }

    final int reviewIndex = reviewWords.indexWhere((w) => w.word == word);
    if (reviewIndex != -1) {
      final List<WordCard> updatedReviewWords = List.from(reviewWords);
      updatedReviewWords[reviewIndex] = reviewWords[reviewIndex].copyWith(repeatCount: count);
      
      return WordLibrary(
        bookId: bookId,
        newWords: newWords,
        reviewWords: updatedReviewWords,
      );
    }

    return this;
  }

  // 添加单词到新词词库
  WordLibrary addToNew(WordCard word) {
    if (newWords.any((w) => w.word == word.word) || 
        reviewWords.any((w) => w.word == word.word)) {
      return this;
    }

    final WordCard newWord = word.copyWith(status: 'new', repeatCount: 0);
    final List<WordCard> updatedNewWords = List.from(newWords)..add(newWord);

    return WordLibrary(
      bookId: bookId,
      newWords: updatedNewWords,
      reviewWords: reviewWords,
    );
  }

  // 获取艾宾浩斯记忆曲线序列
  List<WordCard> generateReviewSequence() {
    final List<WordCard> sequence = [];
    
    for (final word in newWords) {
      // 根据重复次数决定出现的次数
      final int remainingRepeats = 5 - word.repeatCount;
      for (int i = 0; i < remainingRepeats; i++) {
        sequence.add(word);
      }
    }
    
    // 打乱顺序
    sequence.shuffle();
    return sequence;
  }
} 